package cn.itcast.listener;

import cn.itcast.enums.CollegeStatusEnum;
import cn.itcast.enums.DegreeEnum;
import cn.itcast.enums.GenderEnum;
import cn.itcast.pojo.Students;
import cn.itcast.service.StudentsService;
import com.alibaba.excel.context.AnalysisContext;

import com.alibaba.excel.read.listener.ReadListener;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

@Slf4j
@RequiredArgsConstructor
public class StudentDataListener implements ReadListener<Students> {
    private final StudentsService studentService;
    // 每100条存储一次数据库
    private static final int BATCH_SIZE = 100;
    private int count = 0;
    @Override
    public void invoke(Students student, AnalysisContext context) {
        // 设置系统字段（转换已在自定义转换器中完成）
        student.setCreateTime(new Date());
        student.setUpdateTime(new Date());

        // 保存数据
        studentService.saveStudent(student);
        count++;

        if (count % BATCH_SIZE == 0) {
            log.info("已处理 {} 条数据", count);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        log.info("Excel导入完成，共处理 {} 条数据", count);
    }
}